﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ym.Core.Communi
{
    public class ByteUtility
    {
        static ByteUtility()
        {
        }

        /// <summary>
        /// 获取Short低位
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte LowByte(short value)
        {
            return (byte)(value & 0xff);
        }

        /// <summary>
        /// 获取Short高位
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte HightByte(short value)
        {
            return (byte)(value >> 8);
        }

        /// <summary>
        /// 获取Short低位,超过256的忽略
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte LowByte(int value)
        {
            return LowByte(ToShort(value));
        }

        /// <summary>
        /// 获取Short高位,超过256的忽略
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte HightByte(int value)
        {
            return HightByte(ToShort(value));
        }

        static short ToShort(int value)
        {
            return (short)(value & 0xffff);
        }

        public static short ToShort(byte hight, byte low)
        {
            return (short)((hight << 8) + low);
        }

        /// <summary> 
        /// 字节数组转换成16进制字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
            {
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
            }
            return sb.ToString().Trim().ToUpper();
        }

    }
}
